package com.woniuxy.microcontentserver.sectionmsg.controller;


import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.microcontentserver.bean.SectionBean;
import com.woniuxy.microcontentserver.sectionmsg.service.ISectioinService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/section")
public class SectionController {

    @Resource
    private ISectioinService sectioinServiceImpl;

    /**
     * 查询所有栏目
     * @param sectionBean
     * @return
     */
    @GetMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    /*public List<SectionBean> findAll(SectionBean sectionBean){
        System.out.println(sectionBean);
        ResultUtil resultUtil=new ResultUtil();
        List<SectionBean> sectionBeans=sectioinServiceImpl.findAll(sectionBean);
        System.out.println(sectionBeans);
        return sectionBeans;
    }*/
    public ResultMsg findAll(SectionBean sectionBean){
        ResultMsg resultMsg = new ResultMsg();
        List<SectionBean> sectionBeans= sectioinServiceImpl.findAll(sectionBean);
        if (sectionBeans == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(sectionBeans);
    }

    /**
     * 通过id查询栏目
     * @param id
     * @return
     */
    @GetMapping(value="/findbyid",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg getOneById(Long id){
        ResultMsg resultMsg = new ResultMsg();
        SectionBean sectionBean=sectioinServiceImpl.getOneById(id);
        if (sectionBean == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(sectionBean);
    }

    /**
     * 新增栏目
     * @param sectionBean
     * @return
     */
    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg addSectionBean(SectionBean sectionBean){
        ResultMsg resultMsg=new ResultMsg();
        int addNew=sectioinServiceImpl.addSectionBean(sectionBean);
        if (addNew == 0) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(addNew);
    }

    /**
     * 删除栏目
     * @param id
     * @return
     */
    @PutMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg deleteSectoinBean(Long id){
        ResultMsg resultMsg=new ResultMsg();
        int del= sectioinServiceImpl.deleteSectoinBean(id);
        if (del == 0) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(del);
    }
}
